#  测试登录界面的功能
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import ChatDriver
from selenium.webdriver.support import expected_conditions as EC


class ChatLogin:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:8080/login.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

    # 正常登录状态测试用例
    def ChatLoginSucTest(self):
        # 先清除输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 找到用户输入框并输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        # 找到密码输入框并输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 是否成功登录都会有一个警告弹窗，先跳转到弹窗
        # 为弹窗添加一个等待
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        # 断言文本信息与预期是否一致作为登陆成功的标准
        assert text == "登录成功!"
        time.sleep(1)
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()
        # WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div/div[1]/div[1]')))
        # 登录成功后等待界面刷新
        time.sleep(2)

    # 异常状态下的测试用例
    def ChatLoginFailTest(self):
        # 正确账号,错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "登录失败!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()

        # 错误账号，正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("999")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "登录失败!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()

        # 错误账号，错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("ghekj")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("555")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "登录失败!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()

        # 账号填写，密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()

        # 账号为空，填写密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()

        # 账号密码都为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        time.sleep(1)
        ChatDriver.get_alert_image()
        alert.accept()
